package com.evildog.workspace.generator.gene;


import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class MvcGenerator extends BaseGenerator{

    @Override
    protected InjectionConfig getInjectionConfig() {
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>(16);
                map.put("serviceName", serviceName);
                map.put("basePackage", packageName + "." + moduleName);

                //         application          //
                map.put("modelPackage", packageName + "." + moduleName + ".model"); // model包的地址
                map.put("feignControllerPackage", packageName + "." + moduleName + ".feign"); // server 对 feign实现包的地址
                map.put("voPackage", packageName + "." + moduleName + ".vo"); // vo包地址
                map.put("convertPackage", packageName + "." + moduleName + ".convert");
                map.put("dtoPackage", packageName + "." + moduleName + ".dto");
                map.put("boPackage", packageName + "." + moduleName + ".bo");
                map.put("doPackage", packageName + "." + moduleName + ".entity");

                //         util          //
                map.put("utilPackage", packageName + ".common.util");

                // 导入自定义引入的类
                for (Class<?> aClass : classes) {
                    map.put(aClass.getSimpleName(), aClass.getName());
                    map.put("isTimePack", isTimePack);
                }

                this.setMap(map);
            }
        };

        // 查询详情视图层
        List<FileOutConfig> focList = new ArrayList<>();

        // 查询详情视图层
        focList.add(new FileOutConfig("/template/entityVO.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return getOutputDir() + "/" + packageName.replace(".", "/") + "/" + moduleName + "/vo" + "/" + tableInfo.getEntityName() + "VO" + StringPool.DOT_JAVA;
            }
        });

        // 分页查询视图层
        focList.add(new FileOutConfig("/template/entityPageVO.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return getOutputDir() + "/" + packageName.replace(".", "/") + "/" + moduleName + "/vo" + "/" + tableInfo.getEntityName() + "PageVO" + StringPool.DOT_JAVA;
            }
        });

        // 分页查询参数
        focList.add(new FileOutConfig("/template/entityPageParam.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return getOutputDir() + "/" + packageName.replace(".", "/") + "/" + moduleName + "/model" + "/" + tableInfo.getEntityName() + "PageParam" + StringPool.DOT_JAVA;
            }
        });


        // do
        focList.add(new FileOutConfig("/template/entityPageDO.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return getOutputDir() + "/" + packageName.replace(".", "/") + "/" + moduleName + "/entity" + "/" + tableInfo.getEntityName() + "PageDO" + StringPool.DOT_JAVA;
            }
        });

        // BO service向外传输的数据
        focList.add(new FileOutConfig("/template/entityPageBO.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return getOutputDir() + "/" + packageName.replace(".", "/") + "/" + moduleName + "/bo" + "/" + tableInfo.getEntityName() + "PageBO" + StringPool.DOT_JAVA;
            }
        });

        // PageDTO,controller传入service的数据
        focList.add(new FileOutConfig("/template/entityPageDTO.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return getOutputDir() + "/" + packageName.replace(".", "/") + "/" + moduleName + "/dto" + "/" + tableInfo.getEntityName() + "PageDTO" + StringPool.DOT_JAVA;
            }
        });

        // 分页查询视图层
        focList.add(new FileOutConfig("/template/entityListVO.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return getOutputDir() + "/" + packageName.replace(".", "/") + "/" + moduleName + "/vo" + "/" + tableInfo.getEntityName() + "ListVO" + StringPool.DOT_JAVA;
            }
        });

        // 分页查询参数
        focList.add(new FileOutConfig("/template/entityListParam.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return getOutputDir() + "/" + packageName.replace(".", "/") + "/" + moduleName + "/model" + "/" + tableInfo.getEntityName() + "ListParam" + StringPool.DOT_JAVA;
            }
        });


        // do
        focList.add(new FileOutConfig("/template/entityListDO.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return getOutputDir() + "/" + packageName.replace(".", "/") + "/" + moduleName + "/entity" + "/" + tableInfo.getEntityName() + "ListDO" + StringPool.DOT_JAVA;
            }
        });

        // BO service向外传输的数据
        focList.add(new FileOutConfig("/template/entityListBO.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return getOutputDir() + "/" + packageName.replace(".", "/") + "/" + moduleName + "/bo" + "/" + tableInfo.getEntityName() + "ListBO" + StringPool.DOT_JAVA;
            }
        });

        // ListDTO,controller传入service的数据
        focList.add(new FileOutConfig("/template/entityListDTO.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return getOutputDir() + "/" + packageName.replace(".", "/") + "/" + moduleName + "/dto" + "/" + tableInfo.getEntityName() + "ListDTO" + StringPool.DOT_JAVA;
            }
        });

        // request body
        focList.add(new FileOutConfig("/template/entityParam.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return getOutputDir() + "/" + packageName.replace(".", "/") + "/" + moduleName + "/model" + "/" + tableInfo.getEntityName() + "Param" + StringPool.DOT_JAVA;
            }
        });





        // DTO controller传入service的数据
        focList.add(new FileOutConfig("/template/entityDTO.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return getOutputDir() + "/" + packageName.replace(".", "/") + "/" + moduleName + "/dto" + "/" + tableInfo.getEntityName() + "DTO" + StringPool.DOT_JAVA;
            }
        });



        // BO service向外传输的数据
        focList.add(new FileOutConfig("/template/entityBO.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return getOutputDir() + "/" + packageName.replace(".", "/") + "/" + moduleName + "/bo" + "/" + tableInfo.getEntityName() + "BO" + StringPool.DOT_JAVA;
            }
        });



        // convert
        focList.add(new FileOutConfig("/template/convert.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return getOutputDir() + "/" + packageName.replace(".", "/") + "/" + moduleName + "/convert" + "/" + tableInfo.getEntityName() + "Convert" + StringPool.DOT_JAVA;
            }
        });

        cfg.setFileOutConfigList(focList);
        return cfg;
    }

    @Override
    protected TemplateConfig getTemplateConfig() {
        return new TemplateConfig()
                .setEntity("template/entityDO.java")
                .setController("template/controller.java")
                .setService("template/service.java")
                .setServiceImpl("template/serviceImpl.java")
                .setMapper("template/mapper.java")
                .setXml("template/mapper.xml");
    }

}
